//
//  CTMessageLogic.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/10.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTMessageLogic.h"
#import "CTDebugManager.h"
#import "CTRequestErrorCode.h"
#import "CTRequestUtil.h"
#import "CTUrl.h"
#import "CTFile.h"
#import "CTTextManager.h"
#import "CTUser.h"
#import "CTUserItem.h"
#import "CTLogin.h"
#import "CTGenRequestData.h"

@implementation CTMessageLogic
@synthesize delegate;

- (void)dealloc
{
    [CTDebugManager showDebugLogInfo:@"dealloc : CTMessageLogic Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}


#pragma mark - request -

-(void)messageListRequest:(CTRequestMessageListItem *)item
 successCompletionHandler:(requestSuccessBlock)successBlock
            failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.token forKey:@"token"];
        [param setObject:item.lang forKey:@"lang"];
        [param setObject:item.count forKey:@"count"];
        [param setObject:item.page forKey:@"page"];
        [param setObject:item.timelimit forKey:@"timelimit"];
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"组装消息-个人消息数据错误！"];
    }
    
    [CTRequestUtil sendAsyGetRequest:CT_URL_MESSAGE_NOTIFICATION(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
        BOOL isParserSuccess = [self parserMessageListData:data];
        if (isParserSuccess == YES) {
            successBlock();
        }
        else
        {
            failedBlock();
        }
        
    } withFailedHandler:^(NSError *error) {
        failedBlock();
    }];
}


-(void)messageDeleteRequest:(CTRequestMessageDeleteItem *)item
   successCompletionHandler:(requestSuccessBlock)successBlock
              failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.token forKey:@"token"];
        [param setObject:item.notice_id forKey:@"notice_id"];
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"组装消息-官方消息数据错误！"];
    }
    
    [CTRequestUtil sendAsyPostRequest:CT_URL_MESSAGE_NOTIFICATION_DELETE(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
        BOOL isParserSuccess = [self parserMessageDeleteData:data];
        if (isParserSuccess == YES) {
            successBlock();
        }
        else
        {
            failedBlock();
        }
    } withFailedHandler:^(NSError *error) {
        failedBlock();
    }];
  
}

-(void)messageSwitchRequest:(CTRequestMessageSwitchItem *)item
   successCompletionHandler:(requestSuccessBlock)successBlock
              failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.token forKey:@"token"];
        [param setObject:item.close forKey:@"close"];
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"组装消息开关数据错误！"];
    }
    
    [CTRequestUtil sendAsyPostRequest:CT_URL_MESSAGE_NOTIFICATION_SWITCH(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
        BOOL isParserSuccess = [self parserMessageSwitchData:data];
        if (isParserSuccess == YES) {
            successBlock();
        }
        else
        {
            failedBlock();
        }
    } withFailedHandler:^(NSError *error) {
        failedBlock();
    }];
    
}


#pragma mark - parser -

-(BOOL)parserMessageListData:(NSData *)resultData
{
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                    NSMutableDictionary * infoMutableDic = [infoDic mutableCopy];
                    NSMutableArray * listArr = (NSMutableArray *)[infoMutableDic objectForKey:@"list"];
                    NSMutableArray * newListArr = [NSMutableArray arrayWithCapacity:0];
                    
                    
                    NSString * file_nameStr = @"visitor";
                    CTUserItem * user_Item = [CTUser userInfo];
                    if (user_Item != nil)file_nameStr = [NSString stringWithFormat:@"%@.plist",user_Item.uid];
                    NSMutableDictionary * mesgMutableDic = [CTFile getDicFile:file_nameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/personalMesg"];
                  
                    if (listArr != nil || listArr.count != 0){
                        for (NSDictionary * dic in listArr) {
            
                            
                            NSMutableDictionary * mutableDic = [dic mutableCopy];
                            NSString * noticeID = [NSString stringWithFormat:@"%@",[mutableDic objectForKey:@"notice_id"]];

                            if (mesgMutableDic != nil) {
                                NSMutableArray * mesgArr = (NSMutableArray *)[mesgMutableDic objectForKey:@"list"];
                                if (mesgArr != nil || mesgArr.count != 0) {
                                    
                                    BOOL isHave = NO;
                                    
                                    for (NSDictionary * mesgDicInfo in mesgArr) {
                                        
                                        if ([noticeID isEqualToString:[NSString stringWithFormat:@"%@",[mesgDicInfo objectForKey:@"notice_id"]]]) {
                                            
                                            if ([mesgDicInfo objectForKey:@"isRead"] != nil && [[mesgDicInfo objectForKey:@"isRead"] isEqualToString:@"1"]) {
                                                [mutableDic setObject:@"1" forKey:@"isRead"];
                                            }else{
                                                [mutableDic setObject:@"0" forKey:@"isRead"];
                                            }
                                            
                                            isHave = YES;
                                            [mesgArr removeObject:mesgDicInfo];
                                            break;
                                        }
                                    }
                                    if (isHave == NO) {
                                        [mutableDic setObject:@"0" forKey:@"isRead"];
                                    }
                                }else{
                                    [mutableDic setObject:@"0" forKey:@"isRead"];
                                }
                                
                                mesgArr = nil;
                                
                            }else{
                                [mutableDic setObject:@"0" forKey:@"isRead"];
                            }
                            
                    
                            
                            [newListArr addObject:mutableDic];
                            mutableDic = nil;
                    
                        }
                        
                        listArr = nil;
                        mesgMutableDic = nil;
                        [infoMutableDic setObject:newListArr forKey:@"list"];
                    }
    
                   
                    
                    NSString * fileNameStr = @"visitor";
                    CTUserItem * userItem = [CTUser userInfo];
                    if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@.plist",userItem.uid];
                    
                    //存入文件
                    [CTFile cacheDicFile:infoMutableDic fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/message/personalMesg"];
                    
                }
                else{
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析个人消息数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析个人消息数据错误！"];
    }
    
    return isParserSuccess;
}


-(BOOL)parserMessageDeleteData:(NSData *)resultData
{
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                    NSString * Notic_ID = [infoDic objectForKey:@"notice_id"];
                   
                    //删除数据库通知一条数据
                    [self deleteMessageData:CTMessageLogicTypePersonal withNoticeID:Notic_ID];
                    
                }
                else{
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析消息删除数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析消息删除数据错误！"];
    }
    
    return isParserSuccess;
}


-(BOOL)parserMessageSwitchData:(NSData *)resultData
{
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    NSString * switchStr = [NSString stringWithFormat:@"%@",[deserializedDictionary objectForKey:@"info"]];
                   
                    isParserSuccess = YES;
                    
                    [CTFile cacheFile:switchStr fileName:@"switch.text" saveDoc:CTFileDocLibrary savePath:@"cacheData/message/switch"];
                
                }
                else{
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析消息开关数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析开关数据错误！"];
    }
    
    return isParserSuccess;
}


#pragma mark - 消息数据库操作 -

//增
-(void)addMessageData:(NSDictionary *)info
{
    @try {
        
        if (info == nil) return;
        
        NSDictionary * extrasDic = [info objectForKey:@"extras"];
        NSString * message_type = [NSString stringWithFormat:@"%@",[extrasDic objectForKey:@"message_type"]];
        
        if ([message_type isEqualToString:@"1"]) {
             //个人消息
            
            [self messageListRequest:[CTGenRequestData genRequestMessageListItemWithFlipNumber:1] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([self.delegate respondsToSelector:@selector(personalMessageReceiveSuccessed)]) {
                        [self.delegate personalMessageReceiveSuccessed];
                    }
                });
                [CTDebugManager showDebugLogInfo:@"添加个人消息成功!"];
            } failedHandler:^{
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([self.delegate respondsToSelector:@selector(personalMessageReceiveFailed)]) {
                        [self.delegate personalMessageReceiveFailed];
                    }
                });
                
                [CTDebugManager showDebugLogInfo:@"添加个人消息失败!"];
            }];
            
            [CTDebugManager showDebugLogInfo:@"收到个人消息成功!"];
            
        }else{
            //官方消息
            
            @try {
                
                NSString * fileNameStr;
                CTUserItem * userItem = [CTUser userInfo];
                if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@.plist",userItem.uid];
                else fileNameStr = @"visitor.plist";
                

                
                NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/officialMesg"];
                if (infoMutableArr == nil)infoMutableArr = [NSMutableArray arrayWithCapacity:0];
                
                NSMutableDictionary * mutableInfoDic = [info mutableCopy];
                [mutableInfoDic setObject:@"0" forKey:@"isRead"];
                [infoMutableArr addObject:mutableInfoDic];
                
                [CTFile cacheArrFile:infoMutableArr fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/message/officialMesg"];
                infoMutableArr = nil;
                
                [CTDebugManager showDebugLogInfo:@"添加官方消息成功!"];
                
                if ([self.delegate respondsToSelector:@selector(officialMessageReceiveSuccessed)]) {
                    [self.delegate officialMessageReceiveSuccessed];
                }
                
            }
            @catch (NSException *exception) {
                [CTDebugManager showErrorLogInfo:@"添加官方消息失败!"];
            }
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"添加消息失败!"];
    }
}

//删
-(void)deleteMessageData:(CTMessageLogicType)type withNoticeID:(NSString *)noticeID
{
    if (type == CTMessageLogicTypePersonal) {
        //个人消息
        @try {
            if ([[CTTextManager shareInstance] isBlankString:noticeID] == YES) return;
            
            NSMutableDictionary * infoMutableDic = [CTFile getDicFile:@"personalMesg.plist" fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/personalMesg"];
            if (infoMutableDic == nil || infoMutableDic.count == 0)return;
            
            NSMutableArray * listMutArray = [(NSArray *)[infoMutableDic objectForKey:@"list"] mutableCopy];
            
            for (NSDictionary * infoDic in listMutArray) {
                NSString * notice_id_str = [NSString stringWithFormat:@"%@",[infoDic objectForKey:@"notice_id"]];
                
                if ([notice_id_str isEqualToString:noticeID]) {
                    [listMutArray removeObject:infoDic];
                    [CTDebugManager showDebugLogInfo:@"删除个人消息成功!"];
                    break;
                }
            }
            [infoMutableDic setObject:listMutArray forKey:@"list"];
            
            [CTFile cacheDicFile:infoMutableDic fileName:@"personalMesg.plist" saveDoc:CTFileDocLibrary savePath:@"cacheData/message/personalMesg"];
            
            if ([self.delegate respondsToSelector:@selector(personalMessageRemoveSuccessed)]) {
                [self.delegate personalMessageRemoveSuccessed];
            }
            
        }
        @catch (NSException *exception) {
            [CTDebugManager showErrorLogInfo:@"删除个人消息失败!"];
        }
       
    }else{
        //官方消息
        
        @try {
            
            if ([[CTTextManager shareInstance] isBlankString:noticeID] == YES) return;
            
            NSString * fileNameStr;
            CTUserItem * userItem = [CTUser userInfo];
            if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@.plist",userItem.uid];
            else fileNameStr = @"visitor.plist";
           
            NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/officialMesg"];
            if (infoMutableArr == nil || infoMutableArr.count == 0)return;
            
            for (NSDictionary * dic in infoMutableArr) {
                NSDictionary * extras = [dic objectForKey:@"extras"];
                NSString * notice_id = [NSString stringWithFormat:@"%@",[extras objectForKey:@"notice_id"]];
                
                if ([notice_id isEqualToString:noticeID]) {
                    [infoMutableArr removeObject:dic];
                    [CTDebugManager showDebugLogInfo:@"删除官方消息成功!"];
                    break;
                }
            }
            
            [CTFile cacheArrFile:infoMutableArr fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/message/officialMesg"];
            if ([self.delegate respondsToSelector:@selector(officialMessageRemoveSuccessed)]) {
                [self.delegate officialMessageRemoveSuccessed];
            }
        }
        @catch (NSException *exception) {
            [CTDebugManager showErrorLogInfo:@"删除官方消息失败!"];
        }
        
    }
}


//清空数据
-(void)clearAllMessageData:(CTMessageLogicType)type
{
    if (type == CTMessageLogicTypePersonal)
    {
        @try {
            NSString * fileNameStr = @"visitor";
            CTUserItem * userItem = [CTUser userInfo];
            if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@.plist",userItem.uid];
            [CTFile removeFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/personalMesg"];
            [CTDebugManager showErrorLogInfo:@"清空个人消息成功!"];
            
            [self deleteTemporaryMessageData:CTMessageLogicTemporaryTypePersonal];
            
            if ([self.delegate respondsToSelector:@selector(clearMessageScuueed:)]) {
                [self.delegate clearMessageScuueed:CTMessageLogicTypePersonal];
            }
        }
        @catch (NSException *exception) {
            [CTDebugManager showErrorLogInfo:@"清空个人消息失败!"];
        }
       
    }
    else
    {
        @try {
            NSString * fileNameStr;
            CTUserItem * userItem = [CTUser userInfo];
            if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@.plist",userItem.uid];
            else fileNameStr = @"visitor.plist";
            
            [CTFile removeFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/officialMesg"];
             [CTDebugManager showErrorLogInfo:@"清空官方消息成功!"];
            
            [self deleteTemporaryMessageData:CTMessageLogicTemporaryTypeOfficical];
            
            if ([self.delegate respondsToSelector:@selector(clearMessageScuueed:)]) {
                [self.delegate clearMessageScuueed:CTMessageLogicTypeOfficial];
            }
            
             }
             @catch (NSException *exception) {
                 [CTDebugManager showErrorLogInfo:@"清空官方消息失败!"];
             }
    }
}












//增临时数据
-(void)addTemporaryMessageData:(NSDictionary *)info
{
    @try {
        
        if (info == nil) return;
        
        NSDictionary * extrasDic = [info objectForKey:@"extras"];
        NSString * message_type = [NSString stringWithFormat:@"%@",[extrasDic objectForKey:@"message_type"]];
        
        if ([message_type isEqualToString:@"1"]) {
            //个人消息
            
            NSString * fileNameStr;
            CTUserItem * userItem = [CTUser userInfo];
            if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@_personal.plist",userItem.uid];
            else fileNameStr = @"visitor_personal.plist";
            NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
            if (infoMutableArr == nil || infoMutableArr.count == 0)infoMutableArr = [NSMutableArray arrayWithCapacity:0];
            [infoMutableArr addObject:info];
            [CTFile cacheArrFile:infoMutableArr fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/message/TemporaryMesg"];
            
            
            [CTDebugManager showDebugLogInfo:@"收到临时个人消息成功!"];
            
            if ([self.delegate respondsToSelector:@selector(mesgTemporaryAdded)]) {
                [self.delegate mesgTemporaryAdded];
            }
            
        }else{
            //官方消息
            
            NSString * fileNameStr;
            CTUserItem * userItem = [CTUser userInfo];
            if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@_official.plist",userItem.uid];
            else fileNameStr = @"visitor_official.plist";
            NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
            if (infoMutableArr == nil || infoMutableArr.count == 0)infoMutableArr = [NSMutableArray arrayWithCapacity:0];
            [infoMutableArr addObject:info];
            [CTFile cacheArrFile:infoMutableArr fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/message/TemporaryMesg"];
            
           [CTDebugManager showDebugLogInfo:@"添加官方临时消息成功!"];
            
            if ([self.delegate respondsToSelector:@selector(mesgTemporaryAdded)]) {
                [self.delegate mesgTemporaryAdded];
            }
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"添加临时消息失败!"];
    }
}

//删临时数据
-(void)deleteTemporaryMessageData:(CTMessageLogicTemporaryType)type
{
    if (type == CTMessageLogicTypePersonal) {
        //个人消息
        NSString * fileNameStr;
        CTUserItem * userItem = [CTUser userInfo];
        if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@_personal.plist",userItem.uid];
        else fileNameStr = @"visitor_personal.plist";
        
        [CTFile removeFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
        
        if ([self.delegate respondsToSelector:@selector(mesgTemporaryRemoved)]) {
            [self.delegate mesgTemporaryRemoved];
        }
        
    }else{
        //官方消息
        
        NSString * fileNameStr;
        CTUserItem * userItem = [CTUser userInfo];
        if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@_official.plist",userItem.uid];
        else fileNameStr = @"visitor_officical.plist";
        
        [CTFile removeFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
        
        if ([self.delegate respondsToSelector:@selector(mesgTemporaryRemoved)]) {
            [self.delegate mesgTemporaryRemoved];
        }
    }
}

//获取临时数据的数量
-(NSInteger)mesgTemporaryDataCount:(CTMessageLogicTemporaryType)type
{
    NSInteger temporaryCount = 0;
    
    if (type == CTMessageLogicTemporaryTypePersonal) {
        //个人临时未读消息总数
        
        NSString * fileNameStr;
        CTUserItem * userItem = [CTUser userInfo];
        if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@_personal.plist",userItem.uid];
        else fileNameStr = @"visitor_personal.plist";
        NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
        if (infoMutableArr == nil || infoMutableArr.count == 0) temporaryCount = 0;
        else temporaryCount = infoMutableArr.count;
        
    }else if (type == CTMessageLogicTemporaryTypeOfficical){
        //官方临时未读消息总数
        
        NSString * fileNameStr;
        CTUserItem * userItem = [CTUser userInfo];
        if (userItem != nil)fileNameStr = [NSString stringWithFormat:@"%@_official.plist",userItem.uid];
        else fileNameStr = @"visitor_official.plist";
        NSMutableArray * infoMutableArr = [CTFile getArrFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
        if (infoMutableArr == nil || infoMutableArr.count == 0) temporaryCount = 0;
        else temporaryCount = infoMutableArr.count;
        
    }else if (type == CTMessageLogicTemporaryTypeALL){
        //个人与官方临时未读消息总数
        
        NSString * filePersonalNameStr;
        CTUserItem * userPersonalItem = [CTUser userInfo];
        if (userPersonalItem != nil)filePersonalNameStr = [NSString stringWithFormat:@"%@_personal.plist",userPersonalItem.uid];
        else filePersonalNameStr = @"visitor_personal.plist";
        NSMutableArray * infoPersonalMutableArr = [CTFile getArrFile:filePersonalNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
        
        NSString * fileOfficialNameStr;
        CTUserItem * userOfficialItem = [CTUser userInfo];
        if (userOfficialItem != nil)fileOfficialNameStr = [NSString stringWithFormat:@"%@_official.plist",userOfficialItem.uid];
        else fileOfficialNameStr = @"visitor_official.plist";
        NSMutableArray * infoOfficialMutableArr = [CTFile getArrFile:fileOfficialNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/message/TemporaryMesg"];
        
        
        
        if ((infoOfficialMutableArr == nil || infoOfficialMutableArr.count == 0) && (infoPersonalMutableArr == nil || infoPersonalMutableArr.count == 0)){
            temporaryCount = 0;
        }else if((infoOfficialMutableArr != nil || infoOfficialMutableArr.count != 0) && (infoPersonalMutableArr == nil || infoPersonalMutableArr.count == 0)){
            temporaryCount = infoOfficialMutableArr.count;
        }else if ((infoOfficialMutableArr == nil || infoOfficialMutableArr.count == 0) && (infoPersonalMutableArr != nil || infoPersonalMutableArr.count != 0)){
            temporaryCount = infoPersonalMutableArr.count;
        }else if((infoOfficialMutableArr != nil || infoOfficialMutableArr.count != 0) && (infoPersonalMutableArr != nil || infoPersonalMutableArr.count != 0)){
            temporaryCount = infoPersonalMutableArr.count + infoOfficialMutableArr.count;
        }else{
            temporaryCount = 0;
        }
    }
    
    return temporaryCount;
}

@end
